package com.heima.comment.service.impl;

import com.heima.comment.service.CommentService;
import com.heima.feigns.user.UserFeign;
import com.heima.model.comment.dtos.CommentLikeDto;
import com.heima.model.comment.dtos.CommentSaveDto;
import com.heima.model.comment.pojos.ApComment;
import com.heima.model.comment.pojos.ApCommentLike;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppUserThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Canglan
 * @date 2021/4/23
 */

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private UserFeign userFeign;
    @Override
    public ResponseResult saveComment(CommentSaveDto dto) {
      //1,检查参数
        if (dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getContent()!=null&&dto.getContent().length()>140){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"评论内容不能超过140字");

        }
        //判断是否登录
        ApUser user = AppUserThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //3安全过滤
        // TODO

        //4保存评论
        ApUser apUser = userFeign.findUserById(user.getId().longValue());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"当前登录信息有误");

        }
        ApComment apComment = new ApComment();
        apComment.setAuthorId(apUser.getId());
        apComment.setAuthorName(apUser.getName());
        apComment.setImage(apUser.getImage());
        apComment.setContent(dto.getContent());
        apComment.setEntryId(dto.getArticleId());
        apComment.setCreatedTime(new Date());
        apComment.setUpdatedTime(new Date());
        apComment.setLikes(0);
        apComment.setReply(0);
        apComment.setType((short)0);
        apComment.setFlag((short)0);
        mongoTemplate.insert(apComment);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);


    }

    /**
     * 点赞评论
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(CommentLikeDto dto) {
        //1 参数检查
        if (dto==null||dto.getCommentId()==null||dto.getOperation()<0||dto.getOperation()>1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        }
        //2 判断用户登录
        ApUser user = AppUserThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);

        }
        //3查询评论
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(),ApComment.class);
        if (apComment==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);

        }
        //用户不能重复点赞
        ApCommentLike apCommentLike = mongoTemplate.findOne(Query.query(Criteria.where("authorId").is(user.getId())
                        .and("commentId").is(dto.getCommentId())
                ),
                ApCommentLike.class);
        if (apCommentLike!=null &&
                apCommentLike.getOperation()==0 &&dto.getOperation()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"已点赞");

        }
        if (dto.getOperation()==0){
            //点赞
            //点赞+1
            apComment.setLikes(apComment.getLikes()+1);
            mongoTemplate.save(apComment);
            //保存点赞信息
            apCommentLike = new ApCommentLike();
            apCommentLike.setAuthorId(user.getId());
            apCommentLike.setCommentId(apComment.getId());
            apCommentLike.setOperation(dto.getOperation());
            mongoTemplate.save(apCommentLike);
        }else {
            //取消点赞
            apComment.setLikes(
                    apComment.getLikes()<=0?0:
                            apComment.getLikes()-1
            );
            mongoTemplate.save(apComment);
            //删除点赞评论信息
            mongoTemplate.remove(Query.query(Criteria.where("authorId").is(user.getId())
            .and("commentId").is(apComment.getId())),ApCommentLike.class);

        }
        //4数据返回
        Map<String ,Object>resultMap=new HashMap<>();
        resultMap.put("like",apComment.getLikes());
        return ResponseResult.okResult(resultMap);


    }

}
